package com.train.daily.facade.admin;

import cn.hutool.core.bean.BeanUtil;
import com.train.common.group.AddGroup;
import com.train.common.group.EditGroup;
import com.train.common.group.QueryGroup;
import com.train.common.resp.CommonResp;
import com.train.common.resp.PageResp;
import com.train.daily.dto.train.DailyTrainAddOrEditDTO;
import com.train.daily.dto.train.DailyTrainPageDTO;
import com.train.daily.req.train.DailyTrainAddOrEditReq;
import com.train.daily.req.train.DailyTrainPageReq;
import com.train.daily.resp.train.DailyTrainPageResp;
import com.train.daily.service.DailyTrainService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * dailyTrain Facade 实现类
 * @author baitiaojun
 * @createDate 2025-02-28 17:55:24
 */
@RestController("adminDailyTrainFacade")
@RequiredArgsConstructor
@RequestMapping("/admin/train")
public class DailyTrainFacade {

    private final DailyTrainService dailyTrainService;

    @PostMapping("page")
    public CommonResp pageList(@RequestBody @Validated(QueryGroup.class) DailyTrainPageReq req) {
        DailyTrainPageDTO dailyTrainPageDTO = BeanUtil.copyProperties(req, DailyTrainPageDTO.class);
        PageResp<DailyTrainPageResp> pageResp = dailyTrainService.pageList(dailyTrainPageDTO);
        return CommonResp.ok(pageResp);
    }

    @PostMapping("add")
    public CommonResp add(@RequestBody @Validated(AddGroup.class) DailyTrainAddOrEditReq req) {
        DailyTrainAddOrEditDTO dailyTrainAddOrEditDTO = BeanUtil.copyProperties(req, DailyTrainAddOrEditDTO.class);
        dailyTrainService.addOrEditDailyTrain(dailyTrainAddOrEditDTO);
        return CommonResp.ok();
    }

    @PostMapping("edit")
    public CommonResp edit(@RequestBody @Validated(EditGroup.class) DailyTrainAddOrEditReq req) {
        DailyTrainAddOrEditDTO dailyTrainAddOrEditDTO = BeanUtil.copyProperties(req, DailyTrainAddOrEditDTO.class);
        dailyTrainService.addOrEditDailyTrain(dailyTrainAddOrEditDTO);
        return CommonResp.ok();
    }

    @DeleteMapping("remove/{id}")
    public CommonResp delete(@PathVariable("id") Long id) {
        dailyTrainService.remove(id);
        return CommonResp.ok();
    }

    /**
     * 定时生成日车次信息
     * @param date
     * @return
     */
    @PostMapping("generate-train/{date}")
    public CommonResp generateDailyTrain(@PathVariable("date") String date) {
        dailyTrainService.generateDailyTrain(date);
        return CommonResp.ok();
    }
}
